#ifndef OACTION_H
#define OACTION_H

#include <QAction>
#include <QStateMachine>

#include "ostd.h"
#include "odescriptors.h"

class OAction;
class OTask;
class OWindow;

typedef QMap<QPair<OTask*, int>, OTask*> OTaskRoutineMap;
template <> struct ODescriptor<OAction>
{
	QIcon icon;
	QString text;
	QKeySequence shortcut;
	OTask* start;
	OTaskRoutineMap routine;
	QString toolBarName;
	QString menuName;
	QStringList supportedWidgetNames;
};

class OCTOPUS_EXPORT OAction : public QAction
{
	Q_OBJECT
public:
	OAction(const QIcon& icon, const QString& text, const QKeySequence& shortcut,
		const OTask* start, const OTaskRoutineMap& routine, 
		const QString& toolBarName = QString(), 
		const QString& menuName = QString(),
		const QStringList& supportedWidgetNames = QStringList(),
		QObject *parent = 0);
	OAction(const ODescriptor<OAction>& d, QObject* parent = 0);
	~OAction();
	
	inline QString toolBarName() const {return m_toolBarName;}
	inline QString menuName() const {return m_menuName;}

	inline void clearSupportedWidgetNames() {m_supportedWidgetNames.clear();}
	inline void addSupportedWidgetName(const QString& name){m_supportedWidgetNames.append(name);}
	inline void addSupportedWidgetNames(const QStringList& names){m_supportedWidgetNames.append(names);}

	// @return true if current widget is supported, false otherwise 
	bool onCurrentWindowChanged(const OWindow* w);

public slots:
	void startRoutine();
	void reset();
	void translate(int ret);

signals:
	void finished();

private:
	OTask* m_curTask;
	const OTask* m_startTask;
	const OTaskRoutineMap m_routine; // (task_ptr, ret_val)=>next_task_ptr

	QString m_toolBarName;
	QString m_menuName;
	QStringList m_supportedWidgetNames;
};

typedef QList<OAction*> OActionList;

#endif // OACTION_H
